package com.veryedu.hibernate.hbm.dao.impl;

import java.sql.Timestamp;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import com.veryedu.hibernate.hbm.dao.ShowTimeDao;
import com.veryedu.hibernate.hbm.entity.ShowTime;

public class ShowTimeDaoImpl extends BaseDaoImpl implements ShowTimeDao{

	@Override
	public List<ShowTime> findByShowTime(Timestamp startTime, Timestamp endTime) {
		Session session = null;
		List list = null;
		try {
			session = this.getSession();
			
			Criteria cr = session.createCriteria(ShowTime.class);			
			Criterion cron = Restrictions.between("playtime", startTime, endTime);
			cr.add(cron);
			list = cr.list();
			session.flush();
			session.clear();
		} catch (Exception e) {
			e.printStackTrace();
			return list;
		} finally {
			this.closeSession(session);
		}
		return list;
	}

	@Override
	public List<ShowTime> findByRoomName(String roomName) {
		Session session = null;
		List list = null;
		try {
			session = this.getSession();
			/* HQL */
			/*String hql = "from ShowTime as s where s.room.roomname = :roomName";
			Query query = session.createQuery(hql);
			query.setString("roomName", roomName);
			list = query.list();*/
			
			/* QBC */
			Criteria cr = session.createCriteria(ShowTime.class);
			cr.createAlias("room", "r");
			Criterion cron = Restrictions.eq("r.roomname", roomName);
			cr.add(cron);
			list = cr.list();
			
			
			session.flush();
			session.clear();
		} catch (Exception e) {
			e.printStackTrace();
			return list;
		} finally {
			this.closeSession(session);
		}
		return list;
	}
}
